<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>管理系统</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {:widget_st('layui','css')}
  {:widget_st('admin','css')}
</head>
{:do_action('admin_head')}
<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
          <div class="layui-row layui-col-space15">
              <div class="layui-col-md12">
                <div class="layui-collapse" lay-accordion>
                  <div class="layui-colla-item">
                    <div class="layui-colla-title">常用SQL语句</div>
                    <div class="layui-colla-content layui-show_ sql_list"> 
                        <p>查询数据 <span>select * from zf_post;</span></p>
                        <p>更新数据 <span>update zf_post set zf_post_new_field='1' where id=1;</span></p>
                        <p>删除数据 <span>delete from zf_post where id=1;</span></p>

                        <p>新增字段 <span>alter table zf_post add column zf_post_new_field varchar(255);</span></p>
                        <p>删除字段 <span>alter table zf_post drop column zf_post_new_field;</span></p>
                        <p>修改字段 <span>alter table zf_post modify column zf_post_new_field varchar(255);</span></p>
                        <p>修改字段名 <span>alter table zf_post change column zf_post_old_field zf_post_new_field varchar(255);</span></p>
                        <p>删除表并删除数据 <span>truncate table zf_post;</span></p>
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md12">
                  <div class="layui-card">
                      <form class="layui-form info_tj" method="post">
                        <div class="layui-row layui-col-space12 layui-form-item">
                          <div class="layui-col-lg12">
                              <textarea name="sql"  style='width:100%;height:300px;border:1px solid #ccc;font-size:14px;'  ></textarea>
                          </div>
                        </div>
                        
                       
      
                        <div class="layui-form-item">
                            <div style='margin-top:20px;text-align:center;'>
                              <span class="layui-btn  tijiao" lay-submit="" lay-filter="component-form-element">执行SQL</span>
                            </div>
                        </div>
                      </form>
                  </div>            
              </div>
          </div>

      </div>
    </div>
  </div>
  {:widget_st('jq','js')}
  {:widget_st('layui','js')}
  {:widget_st('common','js')} 

  <script>
  layui.use(['element'], function(){
    var $ = layui.$
    ,form = layui.form
    ,element = layui.element;

    function utoa(str) {
      return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
          return String.fromCharCode('0x' + p1);
        }));
    }
    $('.sql_list span').on('click',function(){
      var sql = $(this).text();
      // textarea[name='sql']后面填充
      var old_sql = $("textarea[name='sql']").val();
      $("textarea[name='sql']").val(old_sql + '\n' + sql);
    })
    $(".tijiao").on("click",function(){
      var index = layer.load(1);
      var sql = $("textarea[name='sql']").val();
      var encryptedSql = utoa(sql);
      // 检查加密后的SQL大小
      if (encryptedSql.length > 10485760) { // 假设限制为10MB
        layer.msg("SQL语句太长,请减少内容后重试", {icon: 2});
        return;
      }
      $.ajax({
          type:'post',
          data:{sql:encryptedSql},
          dataType:'json',
          success:function(res){
            layer.close(index);
            if(res.result==1){
              var html = res.msg;
              layer.open({
                type: 1,
                title: '执行结果',
                content:'<div style="padding:20px;">'+ html+'</div>',
                area: ['800px', '600px'],
                btn: ['关闭'],
                btn1: function(index, layero){
                  layer.close(index);
                }
              });
            }else{
              // layer.msg("执行SQL失败", {icon: 2});
                var html = res.msg;
                layer.open({
                    type: 1,
                    title: '执行结果',
                    content:'<div style="padding:20px;">'+ html+'</div>',
                    area: ['800px', '600px'],
                    btn: ['关闭'],
                    btn1: function(index, layero){
                        layer.close(index);
                    }
                });
            }
            
          }
      })

    })
 
   
  });
  </script>
</body>
</html>
{:do_action('admin_js')}
